package cn.blunce;

import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Vector;

/**
 * Created by Blunce on 2015/11/25 0025.
 */
public class MyDic<K, V> extends Dictionary<K, V> {

    private Vector<K> keys;
    private Vector<V> elements;

    public MyDic() {
        this.keys = new Vector<K>();
        this.elements = new Vector<V>();
    }

    public Enumeration<K> keys() {
        return this.keys.elements();
    }

    public Enumeration<V> elements() {
        return this.elements.elements();
    }

    public V get(Object key) {
        int i = keys.indexOf(key);
        return this.elements.get(i);
    }

    public boolean isEmpty() {
        return this.keys.size() == 0;
    }

    public V put(Object key, Object value) {
        int i = keys.indexOf(key);
        if (i != -1) {
            V oldElement = this.elements.elementAt(i);
            this.elements.setElementAt((V) value, i);
            return oldElement;
        } else {
            this.keys.addElement((K) key);
            this.elements.addElement((V) value);
            return null;
        }
    }

    public V remove(Object key) {
        int i = this.keys.indexOf(key);
        if (i != -1) {
            V oldElement = this.elements.elementAt(i);
            this.keys.removeElementAt(i);
            this.elements.removeElementAt(i);
            return oldElement;
        } else {
            return null;
        }

    }

    public int size() {
        return this.keys.size();
    }

    public static void main(String[] args) {
        MyDic<String, String> dic = new MyDic<String, String>();
        dic.put("1", "0001");
        dic.put("2", "0002");
        dic.put("3", "0003");
        dic.put("4", "0004");
        System.out.println(dic.get("1") + " " + dic.get("2"));
        System.out.println(dic.remove("2") + " " + dic.remove("4"));
        Enumeration<String> keys = dic.keys();
        while (keys.hasMoreElements()) {
            System.out.print(keys.nextElement() + " ");
        }
        System.out.println();
        Enumeration<String> elements = dic.elements();
        while (elements.hasMoreElements()) {
            System.out.print(elements.nextElement() + " ");
        }
        System.out.println();
    }
}

